import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/person/bloc/my_cjfl_bloc.dart';
import 'package:shopping_group/modle/person/entity/gift_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';

class ChouJiangFuLiViewPage extends StatefulWidget {
  @override
  _ChouJiangFuLiViewPageState createState() => _ChouJiangFuLiViewPageState();
}

class _ChouJiangFuLiViewPageState extends State<ChouJiangFuLiViewPage> {
  List<GiftEntity> _listEntity = [];
  ScrollController _scrollController = ScrollController();
  MyCjflBloc _bloc;
  StreamSubscription _refresSubscription;
  @override
  void initState() {
    super.initState();
    _bloc = MyCjflBloc();
    _bloc.initDate.add(null);
    _initListener();

    _refresSubscription = eventBus.on<RefreshEvent>().listen((event) {
      _bloc.refreshDate.add(null);
    });
  }

  _initListener() {
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _bloc.moreDate.add(_scrollController);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '抽奖记录',
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                // Expanded(
                //     flex: 1,
                //     child: Container(
                //       decoration: BoxDecoration(
                //         color: Colours.blackground_609,
                //       ),
                //       child: Stack(
                //         children: <Widget>[
                //           Container(
                //             alignment: Alignment.centerLeft,
                //             padding: EdgeInsets.symmetric(
                //                 horizontal: 16, vertical: 8),
                //             child: Column(
                //               crossAxisAlignment: CrossAxisAlignment.start,
                //               children: [
                //                 SizedBox(
                //                   height: 16,
                //                 ),
                //                 WidgetUtils.getTextColorLineView('总积分',
                //                     Colours.title_white, Dimens.FONT_SIZE_16),
                //                 _jfView(),
                //               ],
                //             ),
                //           ),
                //         ],
                //       ),
                //     )),
                SizedBox(
                  height: 8,
                ),
                Expanded(
                  child: bodyView(),
                  // flex: 3,
                ),
                SizedBox(
                  height: 8,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget bodyView() {
    return Container(
        color: Colours.transparent,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            Expanded(
              child: _showlistView(),
            ),
          ],
        ));
  }

  Widget _showlistView() {
    return StreamBuilder(
      stream: _bloc.sendData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }
        int count = _listEntity.length;
        if (0 != count) {
          return Container(
            decoration: new BoxDecoration(
              color: Colors.white,
            ),
            child: AnimationLimiter(
              child: RefreshIndicator(
                onRefresh: _pullToRefresh,
                child: ListView.builder(
                  controller: _scrollController,
                  shrinkWrap: true,
                  padding: const EdgeInsets.only(top: 4),
                  itemCount: _listEntity.length,
                  itemBuilder: buildItem,
                ),
              ),
            ),
          );
        } else {
          return Container(
            alignment: Alignment.center,
            child: WidgetUtils.emptyView(
              contexts,
            ),
          );
        }
      },
    );
  }

  //下拉刷新,必须异步async不然会报错
  Future _pullToRefresh() async {
    _listEntity.clear();
    _bloc.refreshDate.add(null);
    return null;
  }

  //ListView的Item
  Widget buildItem(BuildContext context, int index) {
    return Container(
      // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: _xunchaItem(_listEntity[index]),
    );
  }

  Widget _xunchaItem(GiftEntity entity) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.transparent,
      ),
      alignment: FractionalOffset.centerLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 4,
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: WidgetUtils.getTextColorView(
                entity.groupSpecName, Colours.gray_91, Dimens.FONT_SIZE_16),
          ),
          WidgetUtils.itmeStyleView('奖品名称', entity.giftRemark,
              contentAlignment: Alignment.centerRight,
              verticalPadding: 4,
              textColor: Colours.gray_91,
              contentColor: Colours.gray_91),
          WidgetUtils.itmeStyleView('参奖日期', entity.zjDate,
              contentAlignment: Alignment.centerRight,
              verticalPadding: 4,
              isExpanded: false,
              isContentExpanded: true,
              contentColor: Colours.blackground_609,
              textColor: Colours.gray_91),
          WidgetUtils.itmeStyleView(entity.giftGetState, entity.giftGetType,
              contentAlignment: Alignment.centerRight,
              verticalPadding: 4,
              isExpanded: false,
              isContentExpanded: true,
              contentColor: Colours.blackground_609,
              textColor: Colours.gray_91),
          SizedBox(
            height: 4,
          ),
          WidgetUtils.drawCodeDivider(height: 1),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _refresSubscription?.cancel();
    super.dispose();
  }
}
